﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TinyLib.Collision
{
    public class CollisionManager
    {
        public CollisionManager()
        {
        }

        public bool RectVsRectOnTop(Rectangle rectangleA, Rectangle rectangleB)
        {
            if (rectangleA.Intersects(rectangleB)
               && rectangleA.Right > rectangleB.Left
                && rectangleA.Left < rectangleB.Right
                && rectangleA.Bottom < rectangleB.Top + (rectangleA.Height * 0.5f))
            {
                return true;
            }

            return false;
        }

        public bool RectVsRect(Rectangle rectangleA, Rectangle rectangleB)
        {
            if (rectangleA.Intersects(rectangleB))
            {
                return true;
            }

            return false;
        }

        public bool RectVsCircle(Rectangle rectangleA, Rectangle circleB)
        {
            //to be written
            return false;
        }

        public bool CircleVsCircle(Rectangle circleA, Rectangle circleB)
        {
            //to be written
            return false;
        }

        public bool CheckPixelCollision(Rectangle rectangleA, Rectangle rectangleB, Color[] rectdataA, Color[] rectdataB, int alphaToleranceLevel)
        {
            int top = Math.Max(rectangleA.Top, rectangleB.Top);
            int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
            int left = Math.Max(rectangleA.Left, rectangleB.Left);
            int right = Math.Min(rectangleA.Right, rectangleB.Right);

            for (int y = top; y < bottom; y++)
            {
                for (int x = left; x < right; x++)
                {
                    Color colorA = rectdataA[(x - rectangleA.Left) + (y - rectangleA.Top) * rectangleA.Width];
                    Color colorB = rectdataB[(x - rectangleB.Left) + (y - rectangleB.Top) * rectangleB.Width];

                    if (colorA.A > alphaToleranceLevel
                        && colorB.A > alphaToleranceLevel)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

    }
}
